import {EventEmitter, Injectable} from '@angular/core';
import {Application} from "../model/application";
import MTSConstants from "../config/config";
import {Http} from "@angular/http";
import {MaintainRecord} from "../model/maintainRecord";

@Injectable()
export class CamCalendarService {

  servicename:string;

  constructor(
    private http:Http,
  ) { }

  searchEvent: EventEmitter<string> = new EventEmitter();
  listFreshEvent: EventEmitter<string> = new EventEmitter();

  //查询所有用车性质
  getApplicationsByVehicle(startTime:string,endTime:string,vehicleoid:string):Promise<Application[]>{
    const url = `${MTSConstants.HOSTUser}applications/vehicle/calendar?startTime=${startTime}&endTime=${endTime}&vehicleoid=${vehicleoid}&exclude=2,3,4`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getApplicationsByVehicle',error);});
  }

  //查询所有车辆维护记录
  getMaintainRecordsByVehicle(startTime:string,endTime:string,vehicleoid:string):Promise<MaintainRecord[]>{
    const url = `${MTSConstants.HOSTUser}maintain/records/vehicle?startTime=${startTime}&endTime=${endTime}&vehicleoid=${vehicleoid}&exclude=2,3,4`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()})
      .catch((error: any) => {return this.handleError('getMaintainRecordsByVehicle',error);});
  }

  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }

}
